<template>
  <div class="home-view">
    <!-- 跑马灯效果 -->
    <div class="marquee">
      <CarouselView />
    </div>
    <div class="header">
      <div class="title-top">精选美食</div>
      <el-row>
        <el-col :span="8" :offset="8">
          <el-divider>
            <el-icon><star-filled /></el-icon>
          </el-divider>
        </el-col>
      </el-row>
      <div>每日推荐，为您精选最值得品尝的美食！</div>
    </div>
    <el-row :gutter="16">
      <el-col class="home-dishes" @click="handleDetailClick(dish)"
              v-for="(dish, index) in dishes"
              :key="index"
              :span="6"
      >
        <el-card :body-style="{ padding: '10px' }">
          <img :src="dish.imageUrl" class="dish-image" alt="" />
          <el-text truncated>{{ dish.title }}</el-text>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import { StarFilled } from '@element-plus/icons-vue'
import CarouselView from "@/views/home/CarouselView.vue";
import router from "@/router/index.js";
import {getFoodHomeList} from "@/api/food.js";

const dishes = ref([]);

onMounted(() => {
  getDishes();
});

// 获取数据
const getDishes = () => {
  getFoodHomeList().then(res => {
    dishes.value = res.data;
  });
};

const handleDetailClick = (item) => {
  router.push({ name: 'article', params: { id: item.id } })
};


</script>

<style scoped>
.home-view {
  text-align: center;
}

.header {
  margin-bottom: 20px;
}

.dish-image {
  width: 100%;
  height: 200px; /* 固定高度使图像大小一致 */
  object-fit: cover; /* 确保图片覆盖整个容器 */
  border-radius: 8px;
}

.home-dishes {
  margin-bottom: 20px;
}

.title-top {
  font-size: 24px;
  font-weight: bold;
  margin-top: 10px;
}

.marquee {
  margin-top: 20px;
}

</style>
